package com.husd.framework.ddl;

import com.google.common.base.CaseFormat;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 */
public class AutoCodeUtil {

    public static Map<String, String> col2JavaTypeMap = new HashMap<>();

    public static String[] cols = {"bit", "tinyint", "smallint", "mediumint", "int", "bigint", "float", "double", "decimal", "date", "datetime", "timestamp", "time", "year", "char", "varchar", "binary", "varbinary", "tiny text", "text", "medium text", "long text", "tinyblob", "blob", "mediumblob", "longblob", "enum", "set", "geometry", "point", "linestring", "polygon", "multipoint", "multilinestring", "multipolygon", "geometrycollection", "longtext", "mediumtext"};

    //一些不识别的类型，直接用原来的类型，直接报错就行了。
    public static String[] cols_java = {"Integer", "Short", "Short", "Short", "Long", "Long", "Float", "Double", "BigDecimal", "LocalDate", "LocalDateTime", "LocalDateTime", "LocalDateTime", "String", "String", "String", "binary", "varbinary", "String", "String", "String", "String", "tinyblob", "blob", "mediumblob", "longblob", "enum", "set", "geometry", "point", "linestring", "polygon", "multipoint", "multilinestring", "multipolygon", "geometrycollection", "String", "String"};

    private static Set<String> baseType = new HashSet<>();

    static {

        baseType.add("String");
        baseType.add("Integer");
        baseType.add("Long");
        baseType.add("BigDecimal");
        baseType.add("Date");
        baseType.add("LocalDate");
        baseType.add("LocalDateTime");
        baseType.add("Boolean");

        baseType.add("List<String>");
        baseType.add("List<Integer>");
        baseType.add("List<Long>");
        baseType.add("List<BigDecimal>");
        baseType.add("List<Date>");
        baseType.add("List<LocalDate>");
        baseType.add("List<LocalDateTime>");
        baseType.add("List<Boolean>");

        for (int i = 0; i < cols.length; i++) {
            col2JavaTypeMap.put(cols[i], cols_java[i]);
        }
    }

    public static final boolean DEBUG = true;

    public static String getJavaType(String col) {

        return col2JavaTypeMap.getOrDefault(col, col);
    }

    public static String getCamelName(String name) {

        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
    }

    public static String firstUpper(String str) {

        char[] arr = str.toCharArray();
        arr[0] = Character.toUpperCase(arr[0]);
        return String.valueOf(arr);
    }

    public static String firstLowerCase(String str) {

        char[] arr = str.toCharArray();
        arr[0] = Character.toLowerCase(arr[0]);
        return String.valueOf(arr);
    }

    public static String findArrayType(String fieldType) {

        if (fieldType == null || fieldType.length() <= 5) {
            return fieldType;
        }
        // List<String>
        if (StringUtils.equalsIgnoreCase("List<", fieldType.substring(0, 5))) {
            String sub = fieldType.substring(5);
            return sub.substring(0, sub.length() - 1);
        } else {
            return fieldType;
        }
    }

    public static boolean isBaseType(String name) {

        if (StringUtils.isBlank(name)) {
            throw new RuntimeException("name为空 无法判断是否是Java基本类型");
        }
        if (name.startsWith("java.lang.") || name.startsWith("java.math.")) {
            return true;
        }
        return baseType.contains(name);
    }

    public static String preDealKey(String key) {

        String originalFieldName = key;
        if (originalFieldName.contains("-")) {
            originalFieldName = originalFieldName.replaceAll("-", "");
        }
        if (originalFieldName.contains("_")) {
            String[] arr = originalFieldName.split("_");
            StringBuilder sb = new StringBuilder();
            sb.append(arr[0]);
            for (int i = 1; i < arr.length; i++) {
                sb.append(AutoCodeUtil.firstUpper(arr[i]));
            }
            return sb.toString();
        }
        return originalFieldName;
    }

    /**
     * 去掉特殊符号
     * 首字母大写
     *
     * @param fieldName
     * @return
     */
    public static String name2JavaClassName(String fieldName) {

        if (fieldName.contains(".")) {
            fieldName = fieldName.replaceAll("\\.", "");
        }
        fieldName = firstUpper(fieldName);
        return fieldName;
    }
}
